package com.lenovo.spider.domain;

import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.ToString;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.Serializable;

/**
 * @author <a href="mailto:liuhan3@lenovo.com">HanL(liuhan3)</a>
 * on 2018/3/21 14:50
 */
@Data
@ToString(of = "statusCode")
public class Response implements Serializable {
    /**
     * http状态码
     */
    private int statusCode;
    /**
     * 字符编码
     */
    private String charset;
    /**
     * 真实的链接地址
     */
    private String relUrl;
    /**
     * 请求对象
     */
    private Request request;
    /**
     * 响应内容
     */
    private String content;
    /**
     * 页面文档
     */
    private Document html;

    public Response(int statusCode, String relUrl, Request request) {
        this.statusCode = statusCode;
        this.relUrl = relUrl;
        this.request = request;
    }


    public Response setContent(String content) {
        if (StrUtil.isNotBlank(content)) {
            this.content = content;
            this.html = Jsoup.parse(content);
        }
        return this;
    }
}
